package com.liu.lab03.Servlet;

import com.liu.lab03.dao.MusicDao;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "deleteMusicServlet", value = "/deleteMusic")
public class DeleteMusicServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取当前的id
        int id = Integer.parseInt(request.getParameter("id"));
        // 刘小文  -2420710103---
        // 直接调用修改数据库的方法
        MusicDao dao = new MusicDao();
        int rows = dao.deleteMusic(id);

        // 根据返回值来进行显示
        if (rows > 0) {
            response.sendRedirect("showAllMusics");
        }else {
            PrintWriter out = response.getWriter();
            out.println("删除失败！ <a href='showAllMusics'>返回</a>");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
